<template>
    <div v-if="!item.hidden" class="menu-wrapper">
        <!-- 没有子菜单的内容 -->
        <template v-if="hasOneShowingChild(item.children, item) && (!onlyOneChild.children || onlyOneChild.noShowingChildren) && !item.alwaysShow">
            <Link v-if="item.redirect != 'noRedirect' && onlyOneChild.meta" :to="resolvePath(onlyOneChild.path)">
                <el-menu-item
                    :index="resolvePath(onlyOneChild.path)"
                    class="submenu-title-noDropdown"
                >
                    <i class="el-icon-tickets" />
                    {{ onlyOneChild.meta.title }}
                </el-menu-item>
            </Link>
        </template>
        <!-- 有子菜单的内容 -->
        <el-submenu
            v-else
            :index="resolvePath(item.path)"
        >
            <template slot="title">
                <Item 
                    :icon="item.meta && item.meta.icon"
                    :title="item.meta && item.meta.title"
                />
            </template>
            <SidebarItem
                v-for="child in item.children"
                :key="child.path"
                :item="child"
                :basePath="resolvePath(child.path)"
                class="nest-menu"
            />
        </el-submenu>
    </div>
</template>

<script>
import path from 'path'
import Link from './Link.vue'
import Item from '@/components/Sidebar/Item.vue'
import { Validator } from '../../../node_modules/@bigbighu/cms-utils'
export default {
    name: 'SidebarItem',
    components:{
        Link,
        Item
    },
    props: {
        item: {
            type: Object,
            required: true
        },
        basePath: {
            type: String,
            default: ''
        }
    },
    data(){
        this.onlyOneChild = null
        return {}
    },
    methods:{
        //判断当前菜单是否包含子菜单
        hasOneShowingChild (children = [], parent) {
            //判断子菜单是否存在数据
            const showingChildren = children.filter(item => {
                //item.hidden表示当前菜单是否显示
                if(item.hidden){
                    return false
                }else{
                    return true
                }
            })

            //根据是否包含子菜单来决定页面渲染的数据
            if(showingChildren.length === 0){
                this.onlyOneChild = { ...parent, path: '', noShowingChildren: true }
                return true
            }
            return false
        },
        //返回链接的标识
        resolvePath(routePath){
            //判断跳转链接是否存在
            if(Validator.isExternal(routePath)){
                return routePath
            }
            //判断父级菜单的路径
            if(Validator.isExternal(this.basePath)){
                return this.basePath
            }
            return path.resolve(this.basePath, routePath)
        }
    }
}
</script>

<style lang="less" scoped>
#app .hideSidebar .el-submenu > .el-submenu__title .iconfont {
  margin-left: 18px;
}
#app .el-menu-item {
  display: flex;
  align-items: center;
}
#app .el-menu-item [class^=el-icon-] {
  font-size: 20px;
}
</style>